<?php

namespace common\models\business;

use Yii;

/**
 * This is the model class for table "business_backpig_finish".
 *
 * @property string $id
 * @property string $number
 * @property integer $backorderid
 * @property string $totalbackprice
 * @property string $finishprice
 * @property integer $createtime
 * @property integer $status
 * @property string $file
 */
class BusinessBackpigFinish extends \yii\db\ActiveRecord
{

    //主要用于日志记录
    public $uselog = [
        'tabcomment' => '结算单',      //用于修改了什么的显示
        'mainfield' => 'number',      //主要的显示标题的字段
        'picturefield' => ['file'],        //图片字段
        'timefield' => ['createtime'],       //时间字段
        'unsetfiled' =>[],      //不需要记录的字段
    ];

    const STATUS_DISAGREE = -1;
    const STATUS_DEFAULT = 0;
    const STATUS_AGREE = 1;

    public static $statues = [
        self::STATUS_DISAGREE => '审核不通过',
        self::STATUS_DEFAULT => '未审核',
        self::STATUS_AGREE => '审核通过',
    ];


    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'business_backpig_finish';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['totalbackprice','number','finishprice'],'required'],
            [['id','status'], 'integer'],
            [['totalbackprice', 'finishprice'], 'number'],
            [['number'], 'string', 'max' => 50],
            [['finishuser'], 'string', 'max' => 20],
            [['remark'],'string', 'max' => 2000],
            [['createtime','file'],'safe'],
            [['file'], 'image','extensions' => 'jpg, jpeg, gif, png','minWidth' => 600,'maxWidth' => 1200,'minHeight' =>500,'maxHeight' => 2500],
            ['number','unique'],
        ];
    }


    public function scenarios()
    {
        $scenarios = parent::scenarios();//本行必填，不写的话就会报如上错误
        $scenarios['create'] = ['file'];
        $scenarios['update'] = ['file'];

        return $scenarios;
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'number' => '结算单单号',
            'totalbackprice' => '结算的回购单价格',
            'finishprice' => '决算价格',
            'createtime' => '创建时间',
            'status' => '状态',
            'file' => '附件',
            'backorder' => '结算的回购单',
            'finishuser' => '结算人',
            'remark' => '结算备注'
        ];
    }


    public function beforeSave($insert)
    {
        if($this->isNewRecord){
            $this->createtime = time();
        }
        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }


    public function getBackorder()
    {
        return $this->hasMany(BusinessBackpig::className(),['backfinishid'=>'id']);
    }

}
